package br.gov.cemaden.cliente.inmet;

import br.gov.cemaden.cliente.inmet.exception.NoDataException;
import br.gov.cemaden.library.model.Municipio;
import br.gov.cemaden.library.model.previsao.PrevisaoTempo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.List;
import java.util.concurrent.Callable;
import org.json.simple.parser.ParseException;

/**
 *
 * @author alan
 */
public class ClienteCallable implements Callable<List<PrevisaoTempo>>{

    private final Municipio municipio;
    private final URL url;
    
    public ClienteCallable(Municipio municipio, URL url) {
        this.municipio = municipio;
        this.url = url;
    }
    
    @Override
    public List<PrevisaoTempo> call() throws Exception{
        System.out.println("Beggining "+Thread.currentThread().getName());
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(url.openStream()));
        } catch (IOException ex) {
            System.err.println("BuffereReaderException");
        }
        JSONReader json = new JSONReader();
        try {
            return json.getPrevisoes(reader, municipio);
        } catch (IOException ex) {
            System.err.println("IOException");
        } catch (ParseException ex) {
            System.err.println("parseException");
        } catch (NoDataException ex) {
            System.err.println("NoDataException");
        } catch (java.text.ParseException ex) {
            System.err.println("java.text.ParseException");
        }
        return null;
    }
    
}
